package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.carousel.CarouselLayoutManager;
import com.google.android.material.carousel.CarouselSnapHelper;
import com.google.android.material.carousel.MultiBrowseCarouselStrategy;
import com.google.android.material.carousel.HeroCarouselStrategy;

import java.util.ArrayList;
import java.util.List;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.adapter.CarouselAdapter;
import cn.lrwjz.androidguide.model.CarouselItem;

public class CarouselActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carousel);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Carousel 示例");
        }

        // 准备数据
        List<CarouselItem> items = new ArrayList<>();
        items.add(new CarouselItem(android.R.drawable.ic_menu_gallery, "图片 1"));
        items.add(new CarouselItem(android.R.drawable.ic_menu_gallery, "图片 2"));
        items.add(new CarouselItem(android.R.drawable.ic_menu_gallery, "图片 3"));
        items.add(new CarouselItem(android.R.drawable.ic_menu_gallery, "图片 4"));
        items.add(new CarouselItem(android.R.drawable.ic_menu_gallery, "图片 5"));

        // 设置多浏览轮播
        RecyclerView multiBrowseCarousel = findViewById(R.id.multiBrowseCarousel);
        CarouselLayoutManager multiBrowseLayoutManager = new CarouselLayoutManager(new MultiBrowseCarouselStrategy());
        multiBrowseCarousel.setLayoutManager(multiBrowseLayoutManager);
        CarouselSnapHelper multiBrowseSnapHelper = new CarouselSnapHelper();
        multiBrowseSnapHelper.attachToRecyclerView(multiBrowseCarousel);
        multiBrowseCarousel.setAdapter(new CarouselAdapter(items));

        // 设置英雄轮播
        RecyclerView heroCarousel = findViewById(R.id.heroCarousel);
        CarouselLayoutManager heroLayoutManager = new CarouselLayoutManager(new HeroCarouselStrategy());
        heroCarousel.setLayoutManager(heroLayoutManager);
        CarouselSnapHelper heroSnapHelper = new CarouselSnapHelper();
        heroSnapHelper.attachToRecyclerView(heroCarousel);
        heroCarousel.setAdapter(new CarouselAdapter(items));
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 